package cn.iocoder.yudao.module.expert.controller.admin.projectinfo;

import cn.iocoder.yudao.module.expert.controller.admin.expertinfo.vo.ExpertInfoImportExcelVO;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.expert.controller.admin.projectinfo.vo.*;
import cn.iocoder.yudao.module.expert.dal.dataobject.projectinfo.ProjectInfoDO;
import cn.iocoder.yudao.module.expert.service.projectinfo.ProjectInfoService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 项目信息")
@RestController
@RequestMapping("/expert/project-info")
@Validated
public class ProjectInfoController {

    @Resource
    private ProjectInfoService projectInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建项目信息")
    @PreAuthorize("@ss.hasPermission('expert:project-info:create')")
    public CommonResult<Long> createProjectInfo(@Valid @RequestBody ProjectInfoSaveReqVO createReqVO) {
        return success(projectInfoService.createProjectInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目信息")
    @PreAuthorize("@ss.hasPermission('expert:project-info:update')")
    public CommonResult<Boolean> updateProjectInfo(@Valid @RequestBody ProjectInfoSaveReqVO updateReqVO) {
        projectInfoService.updateProjectInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('expert:project-info:delete')")
    public CommonResult<Boolean> deleteProjectInfo(@RequestParam("id") Long id) {
        projectInfoService.deleteProjectInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('expert:project-info:query')")
    public CommonResult<ProjectInfoRespVO> getProjectInfo(@RequestParam("id") Long id) {
        ProjectInfoDO projectInfo = projectInfoService.getProjectInfo(id);
        return success(BeanUtils.toBean(projectInfo, ProjectInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目信息分页")
    @PreAuthorize("@ss.hasPermission('expert:project-info:query')")
    public CommonResult<PageResult<ProjectInfoRespVO>> getProjectInfoPage(@Valid ProjectInfoPageReqVO pageReqVO) {
        PageResult<ProjectInfoDO> pageResult = projectInfoService.getProjectInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProjectInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目信息 Excel")
    @PreAuthorize("@ss.hasPermission('expert:project-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProjectInfoExcel(@Valid ProjectInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProjectInfoDO> list = projectInfoService.getProjectInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目信息.xls", "数据", ProjectInfoRespVO.class,
                        BeanUtils.toBean(list, ProjectInfoRespVO.class));
    }
    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入项目模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<ProjectInfoImportExcelVO> list =Arrays.asList(
                ProjectInfoImportExcelVO.builder().projectName("项目名称").projectUnit("项目单位").projectContacts("项目联系人").projectPhone("1311111111")
                        .projectMajor("项目所需专家库").major("项目相关专业").projectTime(LocalDateTime.now()).projectAddress("项目地址").projectAmount("123456").projectTendersTime(LocalDateTime.now()).remark("该条为示例").build()
        );
        // 输出
        ExcelUtils.write(response, "项目信息导入模板.xls", "项目列表", ProjectInfoImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入项目")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('expert:project-info:import')")
    public CommonResult<Boolean> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<ProjectInfoImportExcelVO> list = ExcelUtils.read(file, ProjectInfoImportExcelVO.class);
        return success(projectInfoService.importProjectInfoList(list));
    }
}